package com.choosefine.base.attendance.client.device

import Com.FirstSolver.Splash.FaceId
import Com.FirstSolver.Splash.FaceIdAnswer
import Com.FirstSolver.Splash.FaceId_ErrorCode
import Com.FirstSolver.Splash.FaceId_Item
import com.choosefine.base.common.bean.ToolkitBean
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.*
import java.util.regex.Pattern

/**
 * Created by 邱鹏 on 2017/8/28.
 */
@Service
class DeviceApi {
    val log: Logger = LoggerFactory.getLogger(DeviceApi::class.java)
    @Autowired lateinit var toolkit: ToolkitBean
    @Autowired lateinit var deviceApiBean: DeviceApiBean
    /**
     *获取设备的考勤记录
     */
    fun getRecord(): MutableList<String> {
        val recordList: MutableList<String> = mutableListOf()
        try {
            FaceId("192.168.59.180", 9922).use { tcpClient ->
                // 设置通信密码
                tcpClient.secretKey = "123456"   // 注意：密码和设备通信密码一致

                // 获取考勤记录
                val Command = "GetRecord(end_time=\"" + SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date()) + "\")"
                val output = FaceIdAnswer()
                val ErrorCode = tcpClient.Execute(Command, output, "GBK")
                if (ErrorCode == FaceId_ErrorCode.Success) {
                    // 提取单条考勤记录
                    val p = Pattern.compile("\\b(time=.+\\R(?:photo=\"[^\"]+\")*)")
                    val m = p.matcher(output.answer)
                    while (m.find()) {
                        recordList.add(m.group(1))
                    }
                } else {
                    log.info("获取考勤记录失败！")
                }
            }
        } catch (e: RuntimeException) {
            log.info(e.message)
        } catch (e: IOException) {
            log.error("连接设备失败！")
        } finally {
            return recordList
        }
    }

    fun getRecordAndSave() {
        var machneInfoStr = machineinfo()
        deviceApiBean.saveRecord(getRecord(), machneInfoStr)
    }

    /**
     *获取设备信息
     */
    fun machineinfo(): String {
        var result: String = ""
        try {
            FaceId("192.168.59.180", 9922).use({ tcpClient ->
                tcpClient.setSecretKey("123456")
                val output = FaceIdAnswer()
                val ErrorCode = tcpClient.Execute("GetDeviceInfo()", output, "UTF-8")
                if (ErrorCode.equals(FaceId_ErrorCode.Success)) {
                    result = output.answer
                }
            })
        } catch (e: RuntimeException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            return result
        }
    }

    fun machineInfoAndSave() {
        var machineInfo=machineinfo()
        deviceApiBean.saveMachineInfo(machineInfo)
    }

    /**
     *获取设备的身份证号列表
     */
    fun getEmployeeIdList() {
        var result: String? = null
        try {
            FaceId("192.168.59.180", 9922).use({ tcpClient ->
                tcpClient.setSecretKey("123456")
                val output = FaceIdAnswer()
                val ErrorCode = tcpClient.Execute("GetEmployeeId()", output, "UTF-8")
//                val faceId_item = FaceId_Item.GetAllItems(output.answer)
                if (ErrorCode.equals(FaceId_ErrorCode.Success)) {
                    result = output.answer
                }
            })
        } catch (e: RuntimeException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        } catch (e: Exception) {
            e.printStackTrace()
        }

        log.info(result)
    }

    /**
     *获取指定工号的员工信息
     */
    fun getEmployeeById() {
        var result: String? = null
        try {
            FaceId("192.168.59.180", 9922).use({ tcpClient ->
                tcpClient.setSecretKey("123456")
                val output = FaceIdAnswer()
                val ErrorCode = tcpClient.Execute("GetEmployee(id=\"330421199206280812\")", output, "GBK")
                val faceId_item = FaceId_Item.GetAllItems(output.answer)
                if (ErrorCode.equals(FaceId_ErrorCode.Success)) {
                    result = output.answer
                }
            })
        } catch (e: RuntimeException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        } catch (e: Exception) {
            e.printStackTrace()
        }

        log.info(result)
    }

    /**
     *获取指定工号的头像base64
     */
    fun getEmployeePhoto() {
        var result: String? = null
        try {
            FaceId("192.168.59.180", 9922).use({ tcpClient ->
                tcpClient.setSecretKey("123456")
                val output = FaceIdAnswer()
                val ErrorCode = tcpClient.Execute("GetEmployeePhoto(id=\"330421199206280812\")", output, "GBK")
                val faceId_item = FaceId_Item.GetAllItems(output.answer)
                if (ErrorCode.equals(FaceId_ErrorCode.Success)) {
                    result = output.answer
                }
            })
        } catch (e: RuntimeException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        } catch (e: Exception) {
            e.printStackTrace()
        }

        log.info(result)
    }

    /**
     *还在测试中
     */
    fun EnrollEmployee() {
        var result: String? = null
        try {
            FaceId("192.168.59.180", 9922).use({ tcpClient ->
                tcpClient.setSecretKey("123456")
                val output = FaceIdAnswer()
                val ErrorCode = tcpClient.Execute("EnrollEmployee(id=\"330421199206280812\")", output, "GBK")
                log.info(output.answer)
                val faceId_item = FaceId_Item.GetAllItems(output.answer)
                if (ErrorCode.equals(FaceId_ErrorCode.Success)) {
                    result = output.answer
                }
            })
        } catch (e: RuntimeException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        } catch (e: Exception) {
            e.printStackTrace()
        }

        log.info(result)
    }
}